package com.ahdy.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

// 3. 老人与子女关联实体类
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "elderly_family_relations",
        uniqueConstraints = @UniqueConstraint(columnNames = {"elderlyId", "familyId"}))
public class ElderlyFamilyRelation {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @NotNull(message = "老人ID不能为空")
    @Column(nullable = false)
    private Integer elderlyId;

    @NotNull(message = "子女ID不能为空")
    @Column(nullable = false)
    private Integer familyId;

    @NotNull(message = "关系类型不能为空")
    @Enumerated(EnumType.STRING)
    @Column(nullable = false)
    private RelationType relationType;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Column(updatable = false)
    private LocalDateTime createdAt;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime updatedAt;

    @PrePersist
    protected void onCreate() {
        createdAt = LocalDateTime.now();
        updatedAt = LocalDateTime.now();
    }

    @PreUpdate
    protected void onUpdate() {
        updatedAt = LocalDateTime.now();
    }

    public enum RelationType {
        儿子, 女儿, 孙子, 孙女, 其他亲属
    }
}
